package org.jenkinsci.plugins.qywechat.dto;

import hudson.model.Run;
import org.jenkinsci.plugins.qywechat.NotificationUtil;
import org.jenkinsci.plugins.qywechat.model.NotificationConfig;
import hudson.model.AbstractBuild;
import hudson.model.ParameterValue;
import hudson.model.ParametersAction;
import net.sf.json.JSONObject;
import org.apache.commons.lang.StringUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 开始构建的通知信息
 * @author jiaju
 */
public class BuildBeginInfo {

    /**
     * 请求参数
     */
    private Map<String, Object> params = new HashMap<>();

    /**
     * 预计时间，毫秒
     */
    private Long durationTime = 0L;

    /**
     * 本次构建控制台地址
     */
    private String consoleUrl;

    /**
     * 工程名称
     */
    private String projectName;

    /**
     * 工程描述
     */
    private String projectDesc;

    /**
     * 环境名称
     */
    private String topicName = "";

    /**
     * 是否显示change log
     */
    private final boolean showChangeLog;

    private List<String> changeLogs;

    /**
     * 更多自定消息
     */
    private String moreInfo = "";

    public void setChangeLogs(List<String> changeLogs) {
        this.changeLogs = changeLogs;
    }

    public BuildBeginInfo(String projectName, String projectDesc, Run<?, ?> build, NotificationConfig config) {
        //获取请求参数
        List<ParametersAction> parameterList = build.getActions(ParametersAction.class);
        if (parameterList != null && !parameterList.isEmpty()) {
            for (ParametersAction p : parameterList) {
                for (ParameterValue pv : p.getParameters()) {
                    this.params.put(pv.getName(), pv.getValue());
                }
            }
        }
        //预计时间
        if (build.getParent().getEstimatedDuration() > 0) {
            this.durationTime = build.getParent().getEstimatedDuration();
        }
        //控制台地址
        StringBuilder urlBuilder = new StringBuilder();
        String jenkinsUrl = NotificationUtil.getJenkinsUrl();
        if (StringUtils.isNotEmpty(jenkinsUrl)) {
            String buildUrl = build.getUrl();
            urlBuilder.append(jenkinsUrl);
            if (!jenkinsUrl.endsWith("/")) {
                urlBuilder.append("/");
            }
            urlBuilder.append(buildUrl);
            if (!buildUrl.endsWith("/")) {
                urlBuilder.append("/");
            }
            urlBuilder.append("console");
        }
        this.consoleUrl = urlBuilder.toString();
        //工程名称
        this.projectName = projectName;
        //工程名称
        this.projectDesc = projectDesc;
        //环境名称
        if (config.topicName != null) {
            topicName = config.topicName;
        }
        showChangeLog = config.showChangeLog;
        if (StringUtils.isNotEmpty(config.moreInfo)) {
            moreInfo = config.moreInfo;
        }
    }

    public String toJSONString() {
        //参数组装
        StringBuffer paramBuffer = new StringBuffer();
        params.forEach((key, val) -> {
            paramBuffer.append(key);
            paramBuffer.append("=");
            paramBuffer.append(val);
            paramBuffer.append(", ");
        });
        if (paramBuffer.length() == 0) {
            paramBuffer.append("无");
        } else {
            paramBuffer.deleteCharAt(paramBuffer.length() - 2);
        }

        //耗时预计
        String durationTimeStr = "无";
        if (durationTime > 0) {
            Long l = durationTime / (1000 * 60);
            durationTimeStr = l + "分钟";
        }

        //组装内容
        StringBuilder content = new StringBuilder();
        if (StringUtils.isNotEmpty(topicName)) {
            content.append(this.topicName);
        }

        content.append("<font color=\"info\">【").append(this.projectName).append("】</font>开始构建\n");
        content.append(" >构建参数：<font color=\"comment\">").append(paramBuffer).append("</font>\n");
        content.append(" >预计用时：<font color=\"comment\">").append(durationTimeStr).append("</font>\n");
        content.append(" >构建项目：<font color=\"comment\">").append(this.projectDesc).append("</font>\n");
        if (StringUtils.isNotEmpty(moreInfo)) {
            content.append(" >").append(moreInfo).append("\n");
        }
        if (StringUtils.isNotEmpty(this.consoleUrl)) {
            content.append(" >[查看控制台](").append(this.consoleUrl).append(")");
        }

        if (showChangeLog) {
            content.append("\n\n");
            if (changeLogs != null && !changeLogs.isEmpty()) {
                content.append(" ><font color=\"comment\">Change Logs：\n</font>");
                for (int i = 0; i < changeLogs.size(); i++) {
                    content.append(i + 1).append(". ").append(changeLogs.get(i)).append("\n");
                }
            } else {
                content.append(" ><font color=\"comment\">No Changes</font>");
            }
        }

        Map<String, Object> markdown = new HashMap<>();
        markdown.put("content", content.toString());

        Map<String, Object> data = new HashMap<>();
        data.put("msgtype", "markdown");
        data.put("markdown", markdown);

        return JSONObject.fromObject(data).toString();
    }
}
